//
//  Solution42.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2022/11/15.
//  Copyright © 2022 lovivid. All rights reserved.
//

import UIKit

/*
    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图，计算按此排列的柱子，下雨之后能接多少雨水。
    示例 1：
    输入：height = [0,1,0,2,1,0,1,3,2,1,2,1]
    输出：6
    解释：上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图，在这种情况下，可以接 6 个单位的雨水（蓝色部分表示雨水）。
    示例 2：

    输入：height = [4,2,0,3,2,5]
    输出：9
    提示：

    n == height.length
    1 <= n <= 2 * 104
    0 <= height[i] <= 105

    https://leetcode.cn/problems/trapping-rain-water
// */

class Solution42: NSObject {
    func trap(_ height: [Int]) -> Int {
        if height.count < 3 {
            return 0;
        }
        
        //leftMax[i] 表示下标 i 及其左边的位置中，height 的最大高度，
        var leftMax:[Int] = [];
        leftMax.append(height[0])
        for index in 1 ..< height.count {
            if height[index] > leftMax[index-1] {
                leftMax.append(height[index]);
            } else {
                leftMax.append(leftMax[index-1]);
            }
        }
        
        //rightMax[i] 表示下标 i 及其右边的位置中，height 的最大高度。
        var rightMax = height;
        for index in stride(from: height.count-2, through: 0, by: -1) {
            if height[index] > rightMax[index+1] {
                rightMax[index] = height[index];
            } else {
                rightMax[index] = rightMax[index+1];
            }
        }
        
        // 对于下标 i 处能接的雨水量等于 min(leftMax(i), rightMax(i)) - height(i)
        var rtnSum = 0;
        for index in 1 ..< height.count-1 {
            //print ("index=\(index), leftMax[index]=\(leftMax[index]), rightMax[index]=\(rightMax[index]), height[index]=\(height[index]) rtnSum += \(min(leftMax[index], rightMax[index]) - height[index])");
            rtnSum += min(leftMax[index], rightMax[index]) - height[index];
        }

        return rtnSum;
    }
    
    // trapCallStackFull 的 递归展平
    func trapNoRecursiveCall(_ height: [Int]) -> Int {
        if height.count < 3 {
            return 0;
        }
        var rtnSum = 0;
        var rightIndex = height.count - 1;
        while rightIndex > 1 {
            var tmpSum = 0;
            var findLeftEdge = false;
            var leftEdge = 0;
            var leftMax = 0;
            var leftMaxIndex = 0;
            for index in stride(from: rightIndex-1, through:0, by:-1) {
                // 向左搜索，找到左边界
                if height[index] < height[rightIndex] {
                    tmpSum += height[rightIndex] - height[index];
                    if (height[index] > leftMax) {
                        leftMax = height[index];
                        leftMaxIndex = index;
                    }
                } else {
                    //
                    findLeftEdge = true;
                    leftEdge = index;
                    break;
                }
            }
            
            print ("rightIndex=\(rightIndex), rtnSum=\(rtnSum), findLeftEdge=\(findLeftEdge), leftEdge=\(leftEdge), tmpSum=\(tmpSum), leftMax=\(leftMax), leftMaxIndex=\(leftMaxIndex)");
            
            if (findLeftEdge) {
                rtnSum += tmpSum;
                rightIndex = leftEdge;
            } else {
                if leftMax > 0 {
                    var tmpSum = 0;
                    for index in stride(from: rightIndex-1, to:leftMaxIndex, by:-1) {
                        tmpSum += leftMax - height[index];
                    }
                    rtnSum += tmpSum;
                    rightIndex = leftMaxIndex;
                } else {
                    //return trapByRightEdge(&height, rightEdge-1);
                    rightIndex = rightIndex - 1;
                }
            }
        }
        
        return rtnSum;
    }
    
    func trapByRightEdge(_ height: inout [Int], _ rightEdge:Int) -> Int {
        if rightEdge < 2 {
            return 0;
        }
        
        var tmpSum = 0;
        var findLeftEdge = false;
        var leftEdge = 0;
        var leftMax = 0;
        var leftMaxIndex = 0;
        for index in stride(from: rightEdge-1, through:0, by:-1) {
            // 向左搜索，找到左边界
            if height[index] < height[rightEdge] {
                tmpSum += height[rightEdge] - height[index];
                if (height[index] > leftMax) {
                    leftMax = height[index];
                    leftMaxIndex = index;
                }
            } else {
                //
                findLeftEdge = true;
                leftEdge = index;
                break;
            }
        }
        
        print ("rightEdge=\(rightEdge), findLeftEdge=\(findLeftEdge), leftEdge=\(leftEdge), tmpSum=\(tmpSum), leftMax=\(leftMax), leftMaxIndex=\(leftMaxIndex)");
        
        if (findLeftEdge) {
            return tmpSum + trapByRightEdge(&height, leftEdge);
        } else {
            if leftMax > 0 {
                var tmpSum = 0;
                for index in stride(from: rightEdge-1, to:leftMaxIndex, by:-1) {
                    tmpSum += leftMax - height[index];
                }
                return tmpSum + trapByRightEdge(&height, leftMaxIndex);
            }
            return trapByRightEdge(&height, rightEdge-1);
        }
    }
        
    func trapCallStackFull(_ height: [Int]) -> Int {
        if height.count < 3 {
            return 0;
        }
        
//        for rightEdge in stride(from: height.count-1, through:3, by:-1) {
//
//        }
        var hcopy = height;
        
        return self.trapByRightEdge(&hcopy, height.count-1);
    }

    func trapLongTime(_ height: [Int]) -> Int {
        var maxHeight = 0;
        for val in height {
            if val >= maxHeight {
                maxHeight = val;
            }
        }

        var rtnSum:Int = 0;

        for index in stride(from: 1, to: height.count-1, by:1) {
            // 遍历 可能收集到水的下标

            for val in stride(from: height[index]+1, through:maxHeight, by:1) {
                // 遍历在这个下标中 可能被收集到的每一滴水

                // 向左搜索，看下能否碰到边界
                var hasLeftEdge = false;
                for left in stride(from: index-1, through:0, by:-1) {
                    //print ("index=\(index), val=\(val), left=\(left)");
                    if height[left] >= val {
                        hasLeftEdge = true;
                        break;
                    }
                }

                if hasLeftEdge {
                    // 有左边界的情况下，向右搜索，看下能否碰到边界
                    var hasRightEdge = false;
                    
                    for right in index+1 ..< height.count {
                        //print ("index=\(index), val=\(val), right=\(right)");
                        if height[right] >= val {
                            hasRightEdge = true;
                            break;
                        }
                    }
                    if hasRightEdge {
                        // 有左右边界的水 可以被收集
                        rtnSum += 1;
                    }
                }
            }
        }

        return rtnSum;
    }

    // 从左到右扫描，遇到类似 [4,2,3] 的例子会算错
    func trapLeft2Right(_ height: [Int]) -> Int {
        var rtnSum:Int = 0;
        var leftEdge:Int = 0;
        var rightEdge:Int = 0;
        while leftEdge < (height.count - 1) {
            // 从左边界向右搜索，直到找到一个height 大于等于 左边界
            var tmpSum:Int = 0;
            rightEdge = leftEdge+1;
            var anyAddRtn = false;
            while rightEdge < height.count {
                if height[rightEdge] < height[leftEdge] {
                    // 小于 左边界，收集雨水
                    tmpSum += (height[leftEdge] - height[rightEdge]);
                } else {
                    // 大于等于 左边界, 收集的雨水 加入 返回值，
                    //leftEdge = rightEdge;
                    rtnSum += tmpSum;
                    anyAddRtn = true;
                    //print ("leftEdge=\(leftEdge), rightEdge=\(rightEdge), tmpSum=\(tmpSum), rtnSum=\(rtnSum)");
                    break;
                }

                rightEdge += 1;
            }
            // 修改左边界
            if anyAddRtn {
                leftEdge = rightEdge;
            } else {
                leftEdge += 1;
            }
        }

        return rtnSum;
    }
}
